package com.xiu.json;

import com.xiu.json.engine.DefaultJSONEngine;
import com.xiu.json.exception.ParseJsonException;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @Author: tjc
 * @description
 * @Date Created in 16:38 2022-04-10
 */
public class JSON {
    public static JSONObject parseObject(String text) {
        Object obj = new DefaultJSONEngine(text).parse();
        if (obj instanceof JSONObject) {
            return (JSONObject) obj;
        } else {
            throw new ParseJsonException("text is not a json object");
        }
    }

    public static JSONArray parseArray(String text) {
        Object obj = new DefaultJSONEngine(text).parse();
        if (obj instanceof JSONArray) {
            return (JSONArray) obj;
        } else {
            throw new ParseJsonException("text is not a json array");
        }
    }

    public static <T> T toObject(String text, Class<T> clazz) {
        Map<String, Object> elements = new DefaultJSONEngine(text).parseObj();
        // 解析clazz的属性名、类型
        // json 转换到 clazz的属性值


        return null;
    }

    public static <T> List<T> toObjectList(String text, Class<T> clazz) {
        return Collections.emptyList();
    }

    public static String toJson(Object obj) {
        return null;
    }

    public static void main(String[] args) {
        User user = JSON.toObject("{}", User.class);
    }


    public static class User {
        private String name;
        private int age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }
    }
}
